#!/usr/bin/env bash
#
# SCRIPT PARA DESCARGAR MUSICA DE http://www.musica-online.org/
#


r="/var/www/pto_venta/app/webroot/clima";



echo "<!DOCTYPE html>" > $r/cabecera;
echo "<html lang='es'>" >> $r/cabecera;
echo "<head>" >> $r/cabecera;
echo "<link rel='stylesheet' href='clima/css/main_locanto.css' type='text/css' />" > $r/cabecera2;
echo "<link rel='stylesheet' href='clima/css/sprites_weather.css' type='text/css' />" >> $r/cabecera2;
echo "</head>" >> $r/cabecera2;
echo '<body class="clima" lang="es">' >> $r/cabecera2;
echo '<div class="clima" lang="es">' >> $r/cabecera2;



pagina="menu";
#clima menu
echo "ejecutando menu [menu]";
wget -O $r/$pagina -o $r/log.txt http://www.locanto.com.ve/clima/;
menu="html/clima_menu.html";
sed "s/á/\\&aacute;/g" $r/$pagina > $r/2;
sed "s/é/\\&eacute;/g" $r/2 > $r/$pagina;
sed "s/í/\\&iacute;/g" $r/$pagina > $r/2;
sed "s/ó/\\&oacute;/g" $r/2 > $r/$pagina;
sed "s/ú/\\&uacute;/g" $r/$pagina > $r/2;
sed "s/ñ/\\&ntilde;/g" $r/2 > $r/$pagina;
sed "s/°/\\&deg;/g" $r/$pagina > $r/2;
sed "s/Locanto™/ARAUCA/g" $r/2 > $r/$pagina;

cat $r/$pagina | grep '<title' > $r/title;

cat $r/cabecera $r/cabecera2 $r/title > $r/$menu;


cat $r/$pagina | grep -A500 'En Venezuela estar' >> $r/$menu;
cat $r/$menu | grep -B500 'El clima internacional' > $r/2;
cat $r/2 | grep -B500 '</table>' > $r/$menu;
cat $r/$pagina | grep -A100 '</body>' >> $r/$menu;

cat $r/$pagina | grep -A100 '</body>' >> $r/$menu;

sed "s/En Venezuela/<b>Agropatria<\\/b> informa que el pais /g" $r/$menu > $r/2;
mv $r/2 $r/$menu;

cat $r/$menu | grep  '<a href="' > $r/links.txt;
sed 's/"/\n/g' $r/links.txt > $r/$pagina;
cat $r/$pagina | grep  'http://' > $r/links.txt;
sed "s/http:\\/\\///g" $r/links.txt > $r/$pagina;
sed "s/.locanto.com.ve\\/clima\\///g" $r/$pagina > $r/links.txt;


pagina=`less $r/links.txt`;
rm -f $r/links.txt;

for localidad in $pagina
do


sed "s/http:\\/\\/$localidad.locanto.com.ve\\/clima\\//$localidad.clima_actual.html/g" $r/$menu > $r/2;


sed 's/<a href=/<a target="_blank" href=/g' $r/2 > $r/$menu;

echo "ejecutando en [$localidad]";
wget -O $r/$localidad -o $r/log.txt http://$localidad.locanto.com.ve/clima/;
actual="$r/html/$localidad.clima_actual.html";
dia="$r/html/$localidad.clima_dia.html";
#detallado="$r/html/$localidad.clima_detallado.html";


cat $r/$localidad | grep '<title' > $r/title;



sed "s/Locanto™/ARAUCA/g" $r/title > $r/2;
sed "s/ñ/\\&ntilde;/g" $r/2 > $r/title;
sed "s/á/\\&aacute;/g" $r/title > $r/2;
sed "s/é/\\&eacute;/g" $r/2 > $r/title;
sed "s/í/\\&iacute;/g" $r/title > $r/2;
sed "s/ó/\\&oacute;/g" $r/2 > $r/title;
sed "s/ú/\\&uacute;/g" $r/title > $r/2;
sed "s/°/\\&deg;/g" $r/2 > $r/title;

sed "s/á/\\&aacute;/g" $r/$localidad > $r/2;
sed "s/é/\\&eacute;/g" $r/2 > $r/$localidad;
sed "s/í/\\&iacute;/g" $r/$localidad > $r/2;
sed "s/ó/\\&oacute;/g" $r/2 > $r/$localidad;
sed "s/ú/\\&uacute;/g" $r/$localidad > $r/2;
sed "s/ñ/\\&ntilde;/g" $r/2 > $r/$localidad;
sed "s/°/\\&deg;/g" $r/$localidad > $r/2;
mv  $r/2  $r/$localidad;


#clima actual
cat $r/cabecera $r/cabecera2 $r/title > $actual;
cat $r/$localidad | grep -A8 'n actual' >> $actual;
cat $r/$localidad | grep -A100 '</body>' >> $actual;


#clima dia
cat $r/cabecera $r/cabecera2 $r/title >  $dia;
echo '<div class="mybox_title center centered">' >> $dia;
cat $r/$localidad | grep -A49 '<h2>El clima de hoy ' >> $dia;
cat $r/$localidad | grep -A100 '</body>' >> $dia;
echo '</td></tr></table>' >> $dia;

#clima detallado
#cat $r/cabecera $r/cabecera2 $r/title >> $detallado;
echo '<div class="mybox_title">Resumen detallado</div>' >> $dia;
echo '<div class="mybox centered">' >> $dia;
cat $r/$localidad | grep -A279 'Resumen detallado</h2>' >> $dia;
sed "/>Resumen detallado<\\/h2/d" $dia > $r/2;
sed "/<\\/div><br \\/>/d" $r/2 > $dia;
cat $r/$localidad | grep -A100 '</div></body>' >> $dia;
rm -f $r/$localidad;

#break;

done
rm -f $r/2 $r/menu $r/title $r/cabecera $r/cabecera2 $r/log.txt $r/*~;

exit 0